package cn.tedu._03vehicle.controller;

import cn.tedu._03vehicle.base.response.JsonResult;
import cn.tedu._03vehicle.base.response.StatusCode;
import cn.tedu._03vehicle.mapper.UserMapper;
import cn.tedu._03vehicle.pojo.dto.UserLoginParam;
import cn.tedu._03vehicle.pojo.dto.UserRegParam;
import cn.tedu._03vehicle.pojo.entity.User;
import cn.tedu._03vehicle.pojo.vo.UserVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;

@RestController
@RequestMapping("/v1/user/")
@Slf4j
@Api(tags = "01.用户管理模块")
public class UserController {
    @Autowired
    public UserMapper userMapper;

    /**
     * 1.注册功能
     * @param userRegParam
     * @return
     */
    @PostMapping("reg")
    @ApiOperation("注册功能")
    @ApiOperationSupport(order = 10)
    public JsonResult RegUser(@RequestBody @Validated UserRegParam userRegParam) {
         /*
        1.接收客户端传递的数据
        2.校验数据合法性
        3.确认用户名是否被占用[SELECT接口]
            SELECT COUNT(*) FROM user WHERE username=?
            3.1 被占用：return 结束
            3.2 未被占用：插入数据表[Insert接口]，返回
         */

        log.debug("userRegParam={}", userRegParam);

        String username = userRegParam.getUsername();  //拿取用户输入的用户名
        int num = userMapper.selectByUsername(username);  //调用方法查询信息与数据库信息进行比对
        if (num == 0) { //未被占用
            User user = new User();
            BeanUtils.copyProperties(userRegParam, user);
            user.setId(1L);
            user.setCreateTime(new Date());
            user.setUpdateTime(new Date());

            log.debug("user={}", user);
            userMapper.insertUser(user);

            return JsonResult.success();
        } else {//被占用
            return new JsonResult(StatusCode.USERNAME_EXISTS);
        }

    }

    /**
     * 2.登录功能
     * @param userLoginParam
     * @param session
     * @return
     */
    @PostMapping("login")
    @ApiOperation("登陆功能")
    @ApiOperationSupport(order = 20)
    public JsonResult login(@RequestBody UserLoginParam userLoginParam, @ApiIgnore HttpSession session){
         /*
            1.接收数据username password;
            2.用户名密码是否正确
              SELECT id,username FROM user WHERE username=? AND password=?
            3.判断返回;
     */
        log.debug("userLoginParam={}",userLoginParam);

        String username = userLoginParam.getUsername();
        String password = userLoginParam.getPassword();

        //调用接口
        UserVO userVO = userMapper.selectByUsernameAndPassword(username, password);
        if(userVO == null){
            return new JsonResult(StatusCode.USERNAME_PASSWORD_ERROR);
        }

        //存储会话标识:{"user": userVO}
        session.setAttribute("user",userVO);

        //登录成功{"state":2000,"message":"操作成功","data":{"id":101,"username":"xxx"}}
        return JsonResult.success(userVO);

    }

    /**
     * 3.获取当前用户功能
     * @param session
     * @return
     */
    @GetMapping("currentUser")
    @ApiOperation("获取当前用户")
    @ApiOperationSupport(order = 30)
    public JsonResult currentUser(@ApiIgnore HttpSession session){
        UserVO userVO = (UserVO) session.getAttribute("user");

        //{"state":2000, "message":"操作成功", "data":null}
        //{"state":2000, "message":"操作成功", "data":{"id":101,"username":"Lucy"}}
        return JsonResult.success(userVO);
    }


    /**
     * 4.退出登录功能
     * 测试思路:
     *   1.先登录成功;
     *   2.获取当前用户;
     *   3.退出登录;
     *   4.再获取当前用户;
     * @param session
     */
    @GetMapping("logout")
    @ApiOperation("退出登陆功能")
    @ApiOperationSupport(order = 40)
    public void logout(@ApiIgnore HttpSession session){
        //删除会话标识
        session.removeAttribute("user");
    }

}
